package com.hmall.order.config;

import com.hmall.order.constants.MqConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MqConfiguration {

    //订单队列
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable(MqConstants.ITEM_ORDER_QUEUE)
                .ttl(1000 * 60)
                .deadLetterExchange(MqConstants.ITEM_DEAD_EXCHANGE)
                .deadLetterRoutingKey(MqConstants.ITEM_DEAD_KEY)
                .build();
    }
    //订单交换机
    @Bean
    public DirectExchange orderExchange(){
        return new DirectExchange(MqConstants.ITEM_ORDER_EXCHANGE);
    }
    //死信队列
    @Bean
    public Queue deadQueue(){
        return new Queue(MqConstants.ITEM_DEAD_QUEUE);
    }
    //死信交换机
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange(MqConstants.ITEM_DEAD_EXCHANGE);
    }
    //订单绑定
    @Bean
    public Binding orderQueueBindingOrderExchange(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with(MqConstants.ITEM_ORDER_KEY);
    }
    //死信绑定
    @Bean
    public Binding deadQueueBindingDeadExchange(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with(MqConstants.ITEM_DEAD_KEY);
    }
}
